// Timestamp Converter 渲染器
window.TimestampConverterRenderer = class TimestampConverterRenderer {
    static cleanup() {
        // 清理定时器
        if (window.devToolsIntervals) {
            window.devToolsIntervals.forEach(intervalId => {
                clearInterval(intervalId);
            });
            window.devToolsIntervals = [];
        }
    }
    
    static getHTML() {
        return `
            <div class="flex items-center justify-between mb-6">
                <h2 class="text-3xl font-bold text-gray-800">时间戳转换工具</h2>
                <div class="flex items-center space-x-4">
                    <div class="text-sm text-gray-500">
                        <span id="timestamp-status" class="font-medium">就绪</span>
                    </div>
                    <button id="get-current-time" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200 flex items-center">
                        <i data-feather="clock" class="w-4 h-4 mr-2"></i>
                        当前时间
                    </button>
                </div>
            </div>

            <!-- 转换模式选择 -->
            <div class="bg-gray-50 border rounded-lg p-4 mb-6">
                <div class="flex items-center space-x-6 mb-4">
                    <label class="inline-flex items-center">
                        <input type="radio" class="form-radio text-blue-600" name="convert-mode" value="timestamp-to-date" checked>
                        <span class="ml-2 font-medium">时间戳转日期</span>
                    </label>
                    <label class="inline-flex items-center">
                        <input type="radio" class="form-radio text-blue-600" name="convert-mode" value="date-to-timestamp">
                        <span class="ml-2 font-medium">日期转时间戳</span>
                    </label>
                    <label class="inline-flex items-center">
                        <input type="radio" class="form-radio text-blue-600" name="convert-mode" value="batch-convert">
                        <span class="ml-2 font-medium">批量转换</span>
                    </label>
                </div>

                <!-- 工具栏 -->
                <div class="flex flex-wrap items-center gap-3">
                    <button id="convert-timestamp" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200 flex items-center">
                        <i data-feather="refresh-cw" class="w-4 h-4 mr-2"></i>
                        转换
                    </button>
                    <button id="swap-conversion" class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200 flex items-center">
                        <i data-feather="repeat" class="w-4 h-4 mr-2"></i>
                        交换
                    </button>
                    
                    <div class="ml-auto flex items-center space-x-3">
                        <button id="clear-timestamp" class="bg-gray-200 hover:bg-gray-300 text-gray-700 font-bold py-2 px-4 rounded-lg transition duration-200">
                            清空
                        </button>
                        <button id="copy-timestamp" class="bg-gray-200 hover:bg-gray-300 text-gray-700 font-bold py-2 px-4 rounded-lg transition duration-200">
                            复制结果
                        </button>
                        <button id="download-timestamp" class="bg-gray-600 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded-lg transition duration-200">
                            下载
                        </button>
                    </div>
                </div>

                <!-- 转换选项 -->
                <div class="flex flex-wrap items-center gap-4 mt-4 text-sm">
                    <div class="flex items-center space-x-2">
                        <label for="timestamp-unit">时间戳单位:</label>
                        <select id="timestamp-unit" class="border rounded px-2 py-1 text-sm">
                            <option value="auto" selected>自动检测</option>
                            <option value="seconds">秒 (10位)</option>
                            <option value="milliseconds">毫秒 (13位)</option>
                        </select>
                    </div>
                    <div class="flex items-center space-x-2">
                        <label for="output-unit">输出单位:</label>
                        <select id="output-unit" class="border rounded px-2 py-1 text-sm">
                            <option value="seconds" selected>秒</option>
                            <option value="milliseconds">毫秒</option>
                        </select>
                    </div>
                    <div class="flex items-center space-x-2">
                        <input type="checkbox" id="show-relative" class="form-checkbox" checked>
                        <label for="show-relative">显示相对时间</label>
                    </div>
                </div>
            </div>

            <!-- 错误/成功提示 -->
            <div id="timestamp-error" class="hidden bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded-lg mb-4">
                <div class="flex items-center">
                    <i data-feather="alert-circle" class="w-5 h-5 mr-2 text-red-500"></i>
                    <div>
                        <span class="font-medium">转换失败：</span>
                        <span id="timestamp-error-message"></span>
                    </div>
                </div>
            </div>

            <div id="timestamp-success" class="hidden bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded-lg mb-4">
                <div class="flex items-center">
                    <i data-feather="check-circle" class="w-5 h-5 mr-2 text-green-500"></i>
                    <div>
                        <span class="font-medium">转换成功：</span>
                        <span id="timestamp-success-message"></span>
                    </div>
                </div>
            </div>

            <!-- 单个转换模式 -->
            <div id="single-convert-mode" class="space-y-6">
                <!-- 输入区域 -->
                <div>
                    <div class="flex items-center justify-between mb-2">
                        <label class="block text-gray-700 text-sm font-bold" id="input-label">
                            输入时间戳
                        </label>
                        <div class="text-sm text-gray-500">
                            <span id="input-hint">支持10位秒级或13位毫秒级时间戳</span>
                        </div>
                    </div>
                    <div class="relative">
                        <input type="text" id="timestamp-input" 
                            class="w-full px-4 py-3 text-gray-700 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 code-editor" 
                            placeholder="请输入时间戳，例如：1640995200 或 1640995200000">
                        <button id="paste-timestamp" class="absolute top-3 right-3 bg-blue-500 hover:bg-blue-600 text-white px-3 py-1 rounded text-sm transition duration-200">
                            粘贴
                        </button>
                    </div>
                </div>

                <!-- 输出区域 -->
                <div>
                    <div class="flex items-center justify-between mb-2">
                        <label class="block text-gray-700 text-sm font-bold" id="output-label">
                            转换结果
                        </label>
                        <div class="text-sm text-gray-500">
                            <span id="detected-unit">检测单位: 未知</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 border rounded-lg p-4">
                        <div id="conversion-result" class="space-y-3">
                            <div class="text-center text-gray-500 py-8">
                                请输入内容进行转换
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 批量转换模式 -->
            <div id="batch-convert-mode" class="hidden space-y-6">
                <!-- 输入区域 -->
                <div>
                    <div class="flex items-center justify-between mb-2">
                        <label class="block text-gray-700 text-sm font-bold">
                            时间戳列表 (每行一个)
                        </label>
                        <div class="text-sm text-gray-500">
                            <span id="batch-input-count">0</span> 行
                        </div>
                    </div>
                    <textarea id="batch-timestamp-input" 
                        class="w-full px-4 py-3 text-gray-700 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500 code-editor" 
                        rows="8" 
                        placeholder="请输入时间戳列表，每行一个&#10;1640995200&#10;1640995260&#10;1640995320"></textarea>
                </div>

                <!-- 批量结果 -->
                <div>
                    <div class="flex items-center justify-between mb-2">
                        <label class="block text-gray-700 text-sm font-bold">
                            批量转换结果
                        </label>
                        <div class="text-sm text-gray-500">
                            成功: <span id="batch-success-count">0</span> | 
                            失败: <span id="batch-error-count">0</span>
                        </div>
                    </div>
                    <div class="bg-gray-50 border rounded-lg p-4 max-h-96 overflow-y-auto">
                        <div id="batch-result" class="space-y-2">
                            <div class="text-center text-gray-500 py-8">
                                请输入时间戳列表进行批量转换
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 当前时间显示 -->
            <div class="bg-gradient-to-r from-blue-50 to-indigo-50 border border-blue-200 rounded-lg p-4 mt-6">
                <h3 class="text-lg font-semibold text-blue-800 mb-3 flex items-center">
                    <i data-feather="clock" class="w-5 h-5 mr-2"></i>
                    当前时间
                </h3>
                <div class="grid md:grid-cols-2 gap-4 text-sm">
                    <div>
                        <div class="space-y-2">
                            <div>时间戳(秒): <span id="current-timestamp-s" class="font-mono font-medium text-blue-700">-</span></div>
                            <div>时间戳(毫秒): <span id="current-timestamp-ms" class="font-mono font-medium text-blue-700">-</span></div>
                            <div>标准时间: <span id="current-datetime" class="font-mono font-medium text-blue-700">-</span></div>
                        </div>
                    </div>
                    <div>
                        <div class="space-y-2">
                            <div>ISO格式: <span id="current-iso" class="font-mono font-medium text-blue-700">-</span></div>
                            <div>UTC时间: <span id="current-utc" class="font-mono font-medium text-blue-700">-</span></div>
                            <div>星期: <span id="current-weekday" class="font-mono font-medium text-blue-700">-</span></div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 使用说明 -->
            <div class="bg-gradient-to-r from-green-50 to-teal-50 border border-green-200 rounded-lg p-4 mt-6">
                <h3 class="text-lg font-semibold text-green-800 mb-3 flex items-center">
                    <i data-feather="info" class="w-5 h-5 mr-2"></i>
                    功能说明
                </h3>
                <div class="grid md:grid-cols-2 gap-4 text-sm text-green-700">
                    <div>
                        <h4 class="font-semibold mb-2">时间戳转换</h4>
                        <ul class="space-y-1">
                            <li>• 支持10位秒级和13位毫秒级时间戳</li>
                            <li>• 自动检测时间戳单位</li>
                            <li>• 多种日期格式输出</li>
                            <li>• 相对时间显示 (几分钟前/后)</li>
                        </ul>
                    </div>
                    <div>
                        <h4 class="font-semibold mb-2">日期转换</h4>
                        <ul class="space-y-1">
                            <li>• 支持多种日期格式输入</li>
                            <li>• YYYY-MM-DD、MM/DD/YYYY等</li>
                            <li>• 中文格式：2023年1月1日</li>
                            <li>• 批量转换时间戳列表</li>
                        </ul>
                    </div>
                </div>
            </div>
        `;
    }

    static init() {
        const timestampInput = document.getElementById('timestamp-input');
        const batchTimestampInput = document.getElementById('batch-timestamp-input');
        const convertBtn = document.getElementById('convert-timestamp');
        const swapBtn = document.getElementById('swap-conversion');
        const clearBtn = document.getElementById('clear-timestamp');
        const copyBtn = document.getElementById('copy-timestamp');
        const downloadBtn = document.getElementById('download-timestamp');
        const pasteBtn = document.getElementById('paste-timestamp');
        const getCurrentTimeBtn = document.getElementById('get-current-time');
        
        // 模式切换
        const modeRadios = document.querySelectorAll('input[name="convert-mode"]');
        const singleMode = document.getElementById('single-convert-mode');
        const batchMode = document.getElementById('batch-convert-mode');
        
        // 设置控件
        const timestampUnitSelect = document.getElementById('timestamp-unit');
        const outputUnitSelect = document.getElementById('output-unit');
        const showRelativeCheck = document.getElementById('show-relative');
        
        // 状态显示
        const statusSpan = document.getElementById('timestamp-status');
        const errorDiv = document.getElementById('timestamp-error');
        const successDiv = document.getElementById('timestamp-success');
        const errorMessage = document.getElementById('timestamp-error-message');
        const successMessage = document.getElementById('timestamp-success-message');
        
        // 标签和提示
        const inputLabel = document.getElementById('input-label');
        const outputLabel = document.getElementById('output-label');
        const inputHint = document.getElementById('input-hint');
        const detectedUnit = document.getElementById('detected-unit');
        
        // 结果显示
        const conversionResult = document.getElementById('conversion-result');
        const batchResult = document.getElementById('batch-result');
        const batchInputCount = document.getElementById('batch-input-count');
        const batchSuccessCount = document.getElementById('batch-success-count');
        const batchErrorCount = document.getElementById('batch-error-count');

        let currentMode = 'timestamp-to-date';
        let currentResult = '';

        // 更新状态显示
        const updateStatus = (status) => {
            statusSpan.textContent = status;
        };

        // 显示成功消息
        const showSuccess = (message) => {
            successMessage.textContent = message;
            successDiv.classList.remove('hidden');
            errorDiv.classList.add('hidden');
            setTimeout(() => {
                successDiv.classList.add('hidden');
            }, 3000);
        };

        // 显示错误消息
        const showError = (message) => {
            errorMessage.textContent = message;
            errorDiv.classList.remove('hidden');
            successDiv.classList.add('hidden');
        };

        // 隐藏所有提示
        const hideMessages = () => {
            errorDiv.classList.add('hidden');
            successDiv.classList.add('hidden');
        };

        // 更新当前时间显示
        const updateCurrentTime = () => {
            const now = new Date();
            const timestampS = Math.floor(now.getTime() / 1000);
            const timestampMs = now.getTime();
            
            // 安全检查：确保元素存在才更新
            const timestampSEl = document.getElementById('current-timestamp-s');
            const timestampMsEl = document.getElementById('current-timestamp-ms');
            const datetimeEl = document.getElementById('current-datetime');
            const isoEl = document.getElementById('current-iso');
            const utcEl = document.getElementById('current-utc');
            const weekdayEl = document.getElementById('current-weekday');
            
            if (timestampSEl) timestampSEl.textContent = timestampS;
            if (timestampMsEl) timestampMsEl.textContent = timestampMs;
            if (datetimeEl) datetimeEl.textContent = now.toLocaleString('zh-CN');
            if (isoEl) isoEl.textContent = now.toISOString();
            if (utcEl) utcEl.textContent = now.toUTCString();
            if (weekdayEl) weekdayEl.textContent = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][now.getDay()];
        };

        // 更新界面模式
        const updateModeUI = (mode) => {
            currentMode = mode;
            
            if (mode === 'timestamp-to-date') {
                inputLabel.textContent = '输入时间戳';
                outputLabel.textContent = '转换结果';
                inputHint.textContent = '支持10位秒级或13位毫秒级时间戳';
                timestampInput.placeholder = '请输入时间戳，例如：1640995200 或 1640995200000';
                singleMode.classList.remove('hidden');
                batchMode.classList.add('hidden');
            } else if (mode === 'date-to-timestamp') {
                inputLabel.textContent = '输入日期时间';
                outputLabel.textContent = '转换结果';
                inputHint.textContent = '支持多种日期格式：YYYY-MM-DD、MM/DD/YYYY、中文等';
                timestampInput.placeholder = '请输入日期时间，例如：2023-01-01 12:00:00 或 2023年1月1日';
                singleMode.classList.remove('hidden');
                batchMode.classList.add('hidden');
            } else {
                singleMode.classList.add('hidden');
                batchMode.classList.remove('hidden');
            }
            
            // 清空内容
            timestampInput.value = '';
            batchTimestampInput.value = '';
            conversionResult.innerHTML = '<div class="text-center text-gray-500 py-8">请输入内容进行转换</div>';
            batchResult.innerHTML = '<div class="text-center text-gray-500 py-8">请输入时间戳列表进行批量转换</div>';
            detectedUnit.textContent = '检测单位: 未知';
            updateBatchStats();
            hideMessages();
        };

        // 更新批量统计
        const updateBatchStats = () => {
            const lines = batchTimestampInput.value.split('\n').filter(line => line.trim()).length;
            batchInputCount.textContent = lines;
        };

        // 渲染单个转换结果
        const renderSingleResult = (result, inputValue) => {
            if (!result.success) {
                conversionResult.innerHTML = `
                    <div class="text-center text-red-500 py-8">
                        <i data-feather="x-circle" class="w-8 h-8 mx-auto mb-2"></i>
                        <div>${result.error}</div>
                    </div>
                `;
                detectedUnit.textContent = '检测单位: 错误';
                setTimeout(() => feather.replace(), 100);
                return;
            }

            const data = result.data;
            
            if (currentMode === 'timestamp-to-date') {
                detectedUnit.textContent = `检测单位: ${data.detectedUnit === 'seconds' ? '秒' : '毫秒'}`;
                
                const formats = data.formats;
                const showRel = showRelativeCheck.checked;
                
                currentResult = JSON.stringify(formats, null, 2);
                
                conversionResult.innerHTML = `
                    <div class="space-y-4">
                        <div class="grid md:grid-cols-2 gap-4">
                            <div class="space-y-3">
                                <h4 class="font-semibold text-gray-700 border-b pb-1">标准格式</h4>
                                <div class="space-y-2 text-sm">
                                    <div><span class="text-gray-600 w-20 inline-block">ISO:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats.iso}</code></div>
                                    <div><span class="text-gray-600 w-20 inline-block">UTC:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats.utc}</code></div>
                                    <div><span class="text-gray-600 w-20 inline-block">本地:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats.local}</code></div>
                                    ${showRel ? `<div><span class="text-gray-600 w-20 inline-block">相对:</span> <code class="bg-blue-100 px-2 py-1 rounded text-blue-700">${formats.relative}</code></div>` : ''}
                                </div>
                            </div>
                            <div class="space-y-3">
                                <h4 class="font-semibold text-gray-700 border-b pb-1">自定义格式</h4>
                                <div class="space-y-2 text-sm">
                                    <div><span class="text-gray-600 w-20 inline-block">标准:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats['YYYY-MM-DD HH:mm:ss']}</code></div>
                                    <div><span class="text-gray-600 w-20 inline-block">美式:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats['MM/DD/YYYY HH:mm:ss']}</code></div>
                                    <div><span class="text-gray-600 w-20 inline-block">中文:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats.chinese}</code></div>
                                    <div><span class="text-gray-600 w-20 inline-block">星期:</span> <code class="bg-gray-100 px-2 py-1 rounded">${formats.weekday}</code></div>
                                </div>
                            </div>
                        </div>
                        <div class="bg-blue-50 border border-blue-200 rounded-lg p-3">
                            <h4 class="font-semibold text-blue-800 mb-2">详细信息</h4>
                            <div class="grid grid-cols-2 md:grid-cols-4 gap-2 text-sm">
                                <div>年: <span class="font-mono">${formats.details.year}</span></div>
                                <div>月: <span class="font-mono">${formats.details.month}</span></div>
                                <div>日: <span class="font-mono">${formats.details.day}</span></div>
                                <div>时: <span class="font-mono">${formats.details.hour}</span></div>
                                <div>分: <span class="font-mono">${formats.details.minute}</span></div>
                                <div>秒: <span class="font-mono">${formats.details.second}</span></div>
                                <div>季度: <span class="font-mono">Q${formats.details.quarter}</span></div>
                                <div>周次: <span class="font-mono">${formats.details.weekOfYear}</span></div>
                            </div>
                        </div>
                    </div>
                `;
            } else {
                const outputUnit = outputUnitSelect.value;
                const timestamp = data.timestamp;
                const formats = data.formats;
                
                currentResult = String(timestamp);
                detectedUnit.textContent = `输出单位: ${outputUnit === 'seconds' ? '秒' : '毫秒'}`;
                
                conversionResult.innerHTML = `
                    <div class="space-y-4">
                        <div class="text-center">
                            <div class="text-3xl font-bold font-mono text-blue-600 mb-2">${timestamp}</div>
                            <div class="text-gray-600">${outputUnit === 'seconds' ? '秒级时间戳' : '毫秒级时间戳'}</div>
                        </div>
                        <div class="grid md:grid-cols-2 gap-4">
                            <div class="space-y-2">
                                <h4 class="font-semibold text-gray-700 border-b pb-1">时间信息</h4>
                                <div class="space-y-1 text-sm">
                                    <div>标准时间: <code class="bg-gray-100 px-2 py-1 rounded">${formats['YYYY-MM-DD HH:mm:ss']}</code></div>
                                    <div>中文时间: <code class="bg-gray-100 px-2 py-1 rounded">${formats.chinese}</code></div>
                                    <div>星期: <code class="bg-gray-100 px-2 py-1 rounded">${formats.weekday}</code></div>
                                </div>
                            </div>
                            <div class="space-y-2">
                                <h4 class="font-semibold text-gray-700 border-b pb-1">其他单位</h4>
                                <div class="space-y-1 text-sm">
                                    <div>秒级: <code class="bg-gray-100 px-2 py-1 rounded">${Math.floor(data.date.getTime() / 1000)}</code></div>
                                    <div>毫秒级: <code class="bg-gray-100 px-2 py-1 rounded">${data.date.getTime()}</code></div>
                                    <div>ISO: <code class="bg-gray-100 px-2 py-1 rounded">${formats.iso}</code></div>
                                </div>
                            </div>
                        </div>
                    </div>
                `;
            }
            
            setTimeout(() => feather.replace(), 100);
        };

        // 渲染批量转换结果
        const renderBatchResult = (result) => {
            if (!result.success) {
                batchResult.innerHTML = `
                    <div class="text-center text-red-500 py-8">
                        <i data-feather="x-circle" class="w-8 h-8 mx-auto mb-2"></i>
                        <div>${result.error}</div>
                    </div>
                `;
                setTimeout(() => feather.replace(), 100);
                return;
            }

            const data = result.data;
            batchSuccessCount.textContent = data.summary.success;
            batchErrorCount.textContent = data.summary.error;
            
            currentResult = data.results.map(item => {
                if (item.success) {
                    return `${item.input} -> ${item.data.formats['YYYY-MM-DD HH:mm:ss']}`;
                } else {
                    return `${item.input} -> ERROR: ${item.error}`;
                }
            }).join('\n');
            
            const resultHtml = data.results.map(item => {
                if (item.success) {
                    const formats = item.data.formats;
                    return `
                        <div class="bg-green-50 border border-green-200 rounded-lg p-3">
                            <div class="flex items-center justify-between mb-2">
                                <span class="font-mono text-sm text-gray-600">第${item.line}行</span>
                                <i data-feather="check-circle" class="w-4 h-4 text-green-600"></i>
                            </div>
                            <div class="text-sm">
                                <div><strong>输入:</strong> <code class="bg-white px-2 py-1 rounded">${item.input}</code></div>
                                <div><strong>结果:</strong> <code class="bg-white px-2 py-1 rounded">${formats['YYYY-MM-DD HH:mm:ss']}</code></div>
                                <div class="text-gray-600 mt-1">${formats.chinese} (${formats.weekday})</div>
                            </div>
                        </div>
                    `;
                } else {
                    return `
                        <div class="bg-red-50 border border-red-200 rounded-lg p-3">
                            <div class="flex items-center justify-between mb-2">
                                <span class="font-mono text-sm text-gray-600">第${item.line}行</span>
                                <i data-feather="x-circle" class="w-4 h-4 text-red-600"></i>
                            </div>
                            <div class="text-sm">
                                <div><strong>输入:</strong> <code class="bg-white px-2 py-1 rounded">${item.input}</code></div>
                                <div class="text-red-600"><strong>错误:</strong> ${item.error}</div>
                            </div>
                        </div>
                    `;
                }
            }).join('');
            
            batchResult.innerHTML = resultHtml;
            setTimeout(() => feather.replace(), 100);
        };

        // 模式切换事件
        modeRadios.forEach(radio => {
            radio.addEventListener('change', () => {
                updateModeUI(radio.value);
                updateStatus(`切换到${radio.nextElementSibling.textContent}模式`);
            });
        });

        // 输入事件监听
        timestampInput.addEventListener('input', DevToolsUtils.debounce(() => {
            updateStatus('输入中...');
            if (timestampInput.value.trim()) {
                updateStatus('准备转换');
            } else {
                updateStatus('就绪');
                conversionResult.innerHTML = '<div class="text-center text-gray-500 py-8">请输入内容进行转换</div>';
                detectedUnit.textContent = '检测单位: 未知';
            }
        }, 300));

        // 批量输入事件监听
        batchTimestampInput.addEventListener('input', DevToolsUtils.debounce(() => {
            updateBatchStats();
            updateStatus('输入中...');
        }, 300));

        // 转换按钮
        convertBtn.addEventListener('click', () => {
            DevToolsUtils.addLoading(convertBtn);
            hideMessages();
            updateStatus('转换中...');
            
            setTimeout(() => {
                if (currentMode === 'batch-convert') {
                    const options = {
                        unit: timestampUnitSelect.value,
                        timezone: 'local'
                    };
                    
                    const result = TimestampConverter.batchConvert(batchTimestampInput.value, options);
                    renderBatchResult(result);
                    
                    if (result.success) {
                        showSuccess(`批量转换完成，成功 ${result.data.summary.success} 个，失败 ${result.data.summary.error} 个`);
                        updateStatus('批量转换完成');
                    } else {
                        showError(result.error);
                        updateStatus('批量转换失败');
                    }
                } else {
                    const inputValue = timestampInput.value.trim();
                    let result;
                    
                    if (currentMode === 'timestamp-to-date') {
                        const options = {
                            unit: timestampUnitSelect.value,
                            timezone: 'local'
                        };
                        result = TimestampConverter.timestampToDate(inputValue, options);
                    } else {
                        const options = {
                            unit: outputUnitSelect.value,
                            timezone: 'local'
                        };
                        result = TimestampConverter.dateToTimestamp(inputValue, options);
                    }
                    
                    renderSingleResult(result, inputValue);
                    
                    if (result.success) {
                        showSuccess('转换完成');
                        updateStatus('转换完成');
                    } else {
                        showError(result.error);
                        updateStatus('转换失败');
                    }
                }
                
                DevToolsUtils.removeLoading(convertBtn);
                setTimeout(() => feather.replace(), 100);
            }, 200);
        });

        // 交换转换模式
        swapBtn.addEventListener('click', () => {
            const currentRadio = document.querySelector('input[name="convert-mode"]:checked');
            if (currentRadio.value === 'timestamp-to-date') {
                document.querySelector('input[value="date-to-timestamp"]').checked = true;
                updateModeUI('date-to-timestamp');
            } else if (currentRadio.value === 'date-to-timestamp') {
                document.querySelector('input[value="timestamp-to-date"]').checked = true;
                updateModeUI('timestamp-to-date');
            }
            DevToolsUtils.showToast('转换模式已交换！');
        });

        // 获取当前时间
        getCurrentTimeBtn.addEventListener('click', () => {
            const result = TimestampConverter.getCurrentTimestamp(outputUnitSelect.value);
            if (result.success) {
                if (currentMode === 'date-to-timestamp') {
                    timestampInput.value = result.data.date.toISOString().slice(0, 19).replace('T', ' ');
                } else {
                    timestampInput.value = result.data.timestamp;
                }
                DevToolsUtils.showToast('已填入当前时间！');
                updateStatus('已获取当前时间');
                
                // 自动转换
                setTimeout(() => {
                    convertBtn.click();
                }, 100);
            }
        });

        // 清空按钮
        clearBtn.addEventListener('click', () => {
            timestampInput.value = '';
            batchTimestampInput.value = '';
            currentResult = '';
            hideMessages();
            conversionResult.innerHTML = '<div class="text-center text-gray-500 py-8">请输入内容进行转换</div>';
            batchResult.innerHTML = '<div class="text-center text-gray-500 py-8">请输入时间戳列表进行批量转换</div>';
            detectedUnit.textContent = '检测单位: 未知';
            updateBatchStats();
            updateStatus('就绪');
        });

        // 复制按钮
        copyBtn.addEventListener('click', async () => {
            if (currentResult) {
                await DevToolsUtils.copyToClipboard(currentResult);
                DevToolsUtils.showToast('已复制到剪贴板！');
            } else {
                DevToolsUtils.showToast('没有可复制的内容', 'error');
            }
        });

        // 下载按钮
        downloadBtn.addEventListener('click', () => {
            if (currentResult) {
                const filename = prompt('输入文件名:', 'timestamp_result.txt') || 'timestamp_result.txt';
                DevToolsUtils.downloadFile(currentResult, filename, 'text/plain');
                DevToolsUtils.showToast('文件下载成功！');
            } else {
                DevToolsUtils.showToast('没有可下载的内容', 'error');
            }
        });

        // 粘贴按钮
        pasteBtn.addEventListener('click', async () => {
            try {
                const text = await navigator.clipboard.readText();
                if (currentMode === 'batch-convert') {
                    batchTimestampInput.value = text;
                    updateBatchStats();
                } else {
                    timestampInput.value = text;
                }
                DevToolsUtils.showToast('粘贴成功！');
            } catch (err) {
                DevToolsUtils.showToast('粘贴失败，请手动粘贴', 'error');
            }
        });

        // 初始化
        updateModeUI('timestamp-to-date');
        updateCurrentTime();
        updateStatus('就绪');
        
        // 定时更新当前时间
        const timeInterval = setInterval(updateCurrentTime, 1000);
        
        // 存储定时器ID到全局，便于清理
        if (!window.devToolsIntervals) {
            window.devToolsIntervals = [];
        }
        window.devToolsIntervals.push(timeInterval);
        
        // 刷新图标
        setTimeout(() => {
            if (typeof feather !== 'undefined') {
                feather.replace();
            }
        }, 100);
    }
};